//
//  KLMUser.swift
//  KLM
//
//  Created by 朱雨 on 2021/11/12.
//

import Foundation

private let userKey = "userKey"

struct KLMUserCache: Codable {
    var account: String?
    var password: String?
}

struct KLMUser: Codable {
    
    var data: KLMUserData
    struct KLMUserData: Codable {
        var token: String?
        var userInfo: KLMUserInfo
        var loginAccount: String
        var nickname: String
//        var expirationTime: Date?
        
        struct KLMUserInfo: Codable {
            var id: Int///用户ID
            var nickname: String
            var username: String?
            
            var activeStatus: Int? // 活跃状态
            var areacode: String? // 区域编码
            var avatar: String? // 头像
//            var createTime: Int? // 创建时间
            var creator: String? // 创建者
            var deleted: Bool? // 是否已删除
            var loginIp: String? // 登录IP
            var mobile: String? // 手机号码
            var password: String? // 密码
            var sex: Int? // 性别
            var status: Int? // 状态
            var telephone: String? // 电话号码
            var tenantId: Int? // 租户ID
//            var updateTime: Int? // 更新时间
            var updater: String? // 更新者
            var usernamehide: String? // 隐藏后的用户名
        }
        
//        enum CodingKeys: String, CodingKey {
//            case token
//            case userInfo
//            case loginAccount
//            case nickname
//            case expirationTime
//        }
//        
//        init(from decoder: Decoder) throws {
//            let container = try decoder.container(keyedBy: CodingKeys.self)
//            token = try container.decodeIfPresent(String.self, forKey: .token)
//            userInfo = try container.decode(KLMUserInfo.self, forKey: .userInfo)
//            loginAccount = try container.decode(String.self, forKey: .loginAccount)
//            nickname = try container.decode(String.self, forKey: .nickname)
//            if let timestamp = try container.decodeIfPresent(Int.self, forKey: .expirationTime) {
//                expirationTime = Date(timeIntervalSince1970: TimeInterval(timestamp))
//            } else if let dateString = try container.decodeIfPresent(String.self, forKey: .expirationTime) {
//                let dateFormatter = DateFormatter()
//                dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" // 根据实际日期格式调整
//                expirationTime = dateFormatter.date(from: dateString)
//            }
//        }
//        
//        func encode(to encoder: Encoder) throws {
//            var container = encoder.container(keyedBy: CodingKeys.self)
//            try container.encodeIfPresent(token, forKey: .token)
//            try container.encode(userInfo, forKey: .userInfo)
//            
//            try container.encodeIfPresent(loginAccount, forKey: .loginAccount)
//            try container.encode(nickname, forKey: .nickname)
//            
//            if let expirationTime = expirationTime {
//                // 这里假设编码为时间戳，你可以根据需求修改为日期字符串
//                let timestamp = Int(expirationTime.timeIntervalSince1970)
//                try container.encode(timestamp, forKey: .expirationTime)
//            }
//        }
    }
    
    static func cacheUserInfo(user: KLMUserData.KLMUserInfo?)  {
        
        KLMCache.setCache(model: user, key: userKey)
    }
    
    static func getUserInfo() -> KLMUserData.KLMUserInfo? {
        
        return KLMCache.getCache(KLMUserData.KLMUserInfo.self, key: userKey)
    }
    
    static func removeUserInfo() {
        KLMCache.removeObject(key: userKey)
    }
    
    ///缓存登录成功的账号列表
    static func saveAccount(account: KLMUserCache?) {
        guard let account = account else {
            return
        }
        var accountLists: [KLMUserCache] = KLMUser.getAccountLists() ?? [KLMUserCache]()
        if let index = accountLists.firstIndex(where: {$0.account == account.account}) {
            accountLists[index] = account
        } else {
            accountLists.append(account)
        }
        KLMCache.setCache(model: accountLists, key: "accountlist")
    }
    ///获取登录成功的账号列表
    static func getAccountLists() -> [KLMUserCache]? {
        return KLMCache.getCache([KLMUserCache].self, key: "accountlist")
    }
}
